﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Blam_Unity.Classes.Engine.Halo_AA.Theater;
using Blam_Unity.Classes.X360;

///summary///
/// **filmInfo string Array**
/// -   Pos 0 = Film Name
/// -   Pos 1 = Film Author
/// -   Pos 2 = Film Desc
/// -   Pos 3 = Gametype Name
/// -   Pos 4 = Gametype Desc
/// -   Pos 5 = Map Name/Direc
/// -   Pos 6 = Rally Point
///summary////

namespace Blam_Unity.Forms.Halo_AA.Theater
{
    public partial class frmCONFilm : Form
    {
        public string[] playersGT = new string[100];
        public string[] playersST = new string[100];
        public int[] playersOff = new int[100];
        public int playerCount = 0;
        public string fileName;

        public string exGT;
        public string exST;

        public TreeNode prevNode;

        public int selectedNodeOffset;
        public string selectedGTString;
        public string selectedSTString;

        public bool firstNodeSelected;

        public string[] filmInfo = new string[7];

        public frmCONFilm()
        {
            InitializeComponent();
        }

        private void frmCONFilm_Load(object sender, EventArgs e)
        {
            MojosIO mio = new MojosIO(fileName, Endian.Big);
            bool endTable = false;

            this.Text += " " + fileName;

            filmInfo = readContainerHeaderData.readFilmHeader(mio);

            txtFilmName.Text = filmInfo[0];
            txtFilmDesc.Text = filmInfo[2];
            txtFilmAuthor.Text = filmInfo[1];

            txtGametypeName.Text = filmInfo[3];
            txtGametypeDesc.Text = filmInfo[4];

            txtRallyPoint.Text = "";

            lBChest.ClearSelected();
            lBKneeGuard.ClearSelected();
            lBLSholder.ClearSelected();
            lBRSholder.ClearSelected();
            lBVisorColour.ClearSelected();
            lBWrist.ClearSelected();
            lBUtility.ClearSelected();
            lBArmorEffect.ClearSelected();
            lBHelmetAttachment.ClearSelected();
            lBHelmets.ClearSelected();
            lBColourPrimary.ClearSelected();
            lBColourSecondary.ClearSelected();

            txtMap.Text = filmInfo[5];
            if (filmInfo[6] == "") { txtRallyPoint.Enabled = false; }
            else { txtRallyPoint.Text = filmInfo[6]; }

            playersGT = new string[100];
            playersOff = new int[100];
            playersST = new string[100];

            int currPos = 0x2AE21;
            mio.Position = 0x2AE21;
            playerCount = 0;
            prevNode = null;
            exST = "";

            for (int i = 0; i < 100; i++)
            {
                if (endTable == true) { }
                else
                {
                    string gamerTag = mio.ReadString(0x1E, Encoding.Unicode);

                    if (String.IsNullOrWhiteSpace(gamerTag)) { endTable = true; }
                    else
                    {
                        mio.Position = currPos + 0x44;
                        string serviceTag = mio.ReadString(0x08, Encoding.Unicode);
                        int offset = currPos;
                        playersGT[i] = gamerTag;
                        playersST[i] = serviceTag;
                        playersOff[i] = currPos;
                        playerCount++;
                    }
                    mio.Position = currPos + 0x108;
                    currPos = currPos + 0x108;
                }
            }

            tVPlayers.Nodes.Clear();
            if (playerCount == 1) { tVPlayers.Nodes.Add("header", "film - " + filmInfo[0] + " - " + playerCount.ToString() + " player"); }
            else { tVPlayers.Nodes.Add("header", "film - " + filmInfo[0] + " - " + playerCount.ToString() + " players"); }

            int si = 0;
            foreach (string player in playersGT)
            {
                if (String.IsNullOrWhiteSpace(player)) { }
                else
                {
                    TreeNode tn = new TreeNode(player + " - " + playersST[si]);
                    tn.Tag = playersOff[si];

                    tVPlayers.Nodes["header"].Nodes.Add(tn);
                    si++;
                }
            }
            firstNodeSelected = false;
            lblLoadFilm.Text = "select a player...";
            panelLoadFilm.BringToFront();
            
            mio.Close();
        }

        private void tVPlayers_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (firstNodeSelected == false) { }
            else
            {
                prevNode.Text = txtGamertag.Text + " - " + txtServiceTag.Text.ToUpper();
                MojosIO mios = new MojosIO(fileName, Endian.Big);
                savePlayerChunkData.savePlayerDataChunk(mios, selectedNodeOffset, firstNodeSelected, txtGamertag.Text, txtServiceTag.Text, lBHelmetAttachment, lBHelmets, lBChest, lBRSholder, lBLSholder, lBWrist, lBKneeGuard, lBVisorColour, lBArmorEffect);
                mios.Flush();
                mios.Close();
            }
            if (e.Node.Text.StartsWith("film -")) { }
            else
            {
                if (firstNodeSelected == false) { panelPlayerInfo.BringToFront(); }

                MojosIO mio = new MojosIO(fileName, Endian.Big);
                selectedNodeOffset = Convert.ToInt32(e.Node.Tag);
                prevNode = e.Node;

                //Gamertag
                txtGamertag.Text = readPlayerChunkData.readGamertag(mio, selectedNodeOffset, selectedGTString);

                //Service Tag
                txtServiceTag.Text = readPlayerChunkData.readServiceTag(mio, selectedNodeOffset, selectedSTString);

                //Helmets
                ///Varient Base
                lBHelmets.SelectedIndex = readPlayerChunkData.readHelmetVarientBase(mio, selectedNodeOffset);
                ///Attachment
                lBHelmetAttachment.SelectedIndex = readPlayerChunkData.readHelmetVarientAttachment(mio, selectedNodeOffset);

                //Chest
                lBChest.SelectedIndex = readPlayerChunkData.readChestID(mio, selectedNodeOffset);

                //Wrists
                lBWrist.SelectedIndex = readPlayerChunkData.readWristID(mio, selectedNodeOffset);

                //Sholders
                ///Right Sholder
                lBRSholder.SelectedIndex = readPlayerChunkData.readSholderRightID(mio, selectedNodeOffset);
                ///Left Sholder
                lBLSholder.SelectedIndex = readPlayerChunkData.readSholderLeftID(mio, selectedNodeOffset);

                //Knee Guards
                lBKneeGuard.SelectedIndex = readPlayerChunkData.readKneeGuardID(mio, selectedNodeOffset);

                //Visor Colours
                lBVisorColour.SelectedIndex = readPlayerChunkData.readVisorColourID(mio, selectedNodeOffset);

                //Armor Effect
                lBArmorEffect.SelectedIndex = readPlayerChunkData.readArmourEffectID(mio, selectedNodeOffset);

                //Utility
                lBUtility.SelectedIndex = readPlayerChunkData.readUtilityID(mio, selectedNodeOffset);

                //Colours
                ///Primary
                lBColourPrimary.SelectedIndex = readPlayerChunkData.readColourPrimaryID(mio, selectedNodeOffset);
                ///Secondary
                lBColourSecondary.SelectedIndex = readPlayerChunkData.readColourSecondaryID(mio, selectedNodeOffset);
                

                firstNodeSelected = true;
                mio.Flush();
                mio.Close();
            }
        }

        private void lBHelmets_SelectedIndexChanged(object sender, EventArgs e)
        {
            lBHelmetAttachment.Items.Clear();

            if (lBHelmets.SelectedIndex == 0) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "UA", "UA/HUL" }); }
            else if (lBHelmets.SelectedIndex == 1) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "CBRN", "UA/HUL" }); }
            else if (lBHelmets.SelectedIndex == 2) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "Unk1", "Unk2" }); } //Needs Mapping
            else if (lBHelmets.SelectedIndex == 3) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "UA/CNM", "CBRN/HUL" }); }
            else if (lBHelmets.SelectedIndex == 4) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "CBRN/HUL", "CNM-I" }); }
            else if (lBHelmets.SelectedIndex == 5) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "CNM", "UA/HUL" }); }
            else if (lBHelmets.SelectedIndex == 6) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "UA/HUL", "CNM" }); }
            else if (lBHelmets.SelectedIndex == 7) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "UA", "UA/FC" }); }
            else if (lBHelmets.SelectedIndex == 8) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "UA/CNM", "FC-I" }); }
            else if (lBHelmets.SelectedIndex == 9) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "HU/RA", "CBRN/CNM" }); }
            else if (lBHelmets.SelectedIndex == 10) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "CNM", "UA/HUL[3]" }); }
            else if (lBHelmets.SelectedIndex == 11) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "HUL-I", "UA" }); }
            else if (lBHelmets.SelectedIndex == 12) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "CBRN/CNM", "UA/FC-I[2]" }); }
            else if (lBHelmets.SelectedIndex == 13) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "CNM", "UA" }); }
            else if (lBHelmets.SelectedIndex == 14) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "HUL[3]", "UA/HUL[3]", "Haunted!" }); }
            else if (lBHelmets.SelectedIndex == 15) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "UA/HUL", "CBRN/CNM" }); }
            else if (lBHelmets.SelectedIndex == 16) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "FC-I[2]", "UA/HUL-I" }); }
            else if (lBHelmets.SelectedIndex == 17) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "Unk1", "Unk2" }); } //Needs Mapping
            else if (lBHelmets.SelectedIndex == 18) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "HU/RA", "CBRN" }); }
            else if (lBHelmets.SelectedIndex == 19) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "HUL", "UA/HUL[3]" }); }
            else if (lBHelmets.SelectedIndex == 20) { lBHelmetAttachment.Items.AddRange(new Object[] { "Base", "CNM", "UA/HUL[3]" }); }

            try { lBHelmetAttachment.SelectedIndex = 0; }
            catch { }
        }

        public void saveCON()
        {
            MojosIO mio = new MojosIO(fileName, Endian.Big);
            saveContainerHeaderData.saveFilmheader(mio, txtFilmName.Text, txtFilmDesc.Text, txtFilmAuthor.Text, txtGametypeName.Text, txtGametypeDesc.Text, txtMap.Text, txtRallyPoint.Text, (txtRallyPoint.Enabled));
            mio.Flush();
            mio.Close();
            MessageBox.Show("Saved. Now Resign", "Saved.");
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
